﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


namespace KunURP.Editor
{
    public class KunToggleDrawer : KunSubDrawer
    {
        public string Keyword { get; private set; } = null;

        public KunToggleDrawer() : this("", "") {}
        public KunToggleDrawer(string group) : this(group, "") {}
        public KunToggleDrawer(string group, string keyword) : base(group)
        {
            if (keyword != "" && keyword != "_")
            {
                this.Keyword = keyword;
                ShaderDrawUtility.RefreshGroupIsOpen(this.Keyword, false);
                if (ShaderDrawUtility.HaveGroup(this.Group))
                {
                    ShaderDrawUtility.RefreshEnumKey2Group(this.Keyword, this.Group);
                }
            }
            else
            {
                this.Keyword = null;
            }
        }

        public override void DrawShaderProperty(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            bool value = (prop.floatValue != 0.0f);

            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = prop.hasMixedValue;

            value = EditorGUI.Toggle(EditorGUILayout.GetControlRect(), label, value);

            EditorGUI.showMixedValue = false;
            if (EditorGUI.EndChangeCheck())
            {
                prop.floatValue = value ? 1.0f : 0.0f;
                if (this.Keyword != null)
                {
                    ShaderDrawUtility.RefreshMaterialsKeywordEnabled(
                        editor.targets,
                        this.Keyword,
                        value
                    );
                }
            }

            ShaderDrawUtility.RefreshGroupIsOpen(this.Keyword, value);
        }
    }
}
